package com.tpsix.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tpsix.domain.SysMenu;
import com.tpsix.service.SysMenuService;
import com.tpsix.util.R;
import com.tpsix.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Api(value = "menus", tags = "系统菜单资源")
@RestController
@RequestMapping("menus")
@AllArgsConstructor
public class SysMenuResource {

    private final SysMenuService sysMenuService;

    /**
     * 路由列表
     */
    @ApiOperation(value = "获取路由列表")
    @GetMapping("routes")
    public R<List<SysMenu>> routes() {
        return R.ok(sysMenuService.menuTree(SecurityUtils.getUserId()));
    }

    /**
     * 路由列表
     */
    @ApiOperation(value = "获取菜单")
    @GetMapping
    @PreAuthorize("hasAuthority('sys:menu:view')")
    public R<List<SysMenu>> menus() {
        return R.ok(sysMenuService.menuTree(0));
    }

    @PostMapping
    @ApiOperation(value = "添加菜单")
    @PreAuthorize("hasAuthority('sys:menu:add')")
    @ResponseStatus(HttpStatus.CREATED)
    // todo 缺少vm和dto
    public R<Void> add(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "修改菜单")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    @ResponseStatus(HttpStatus.CREATED)
    public R<Void> put(@RequestBody SysMenu sysMenu) {
        sysMenuService.saveOrUpdate(sysMenu);
        return R.ok();
    }

    @DeleteMapping("{id:\\d+}")
    @ApiOperation(value = "删除菜单")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public R<Void> delete(@PathVariable int id) {

        SysMenu sysMenu = sysMenuService.getById(id);
        if (Objects.isNull(sysMenu)) {
            return R.error("数据不存在");
        }
        List<Integer> children = sysMenuService.list(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, id))
                .stream()
                .map(SysMenu::getId)
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(children)) {
            return R.error("菜单下存在子菜单, 不能删除");
        }
        sysMenuService.removeById(id);
        return R.ok();
    }

}
